package com.example.yt_app.Activity;


import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.yt_app.Activity.BaseActivity.BaseActivity;
import com.example.yt_app.AppManager;
import com.example.yt_app.Fragment.AnimationUseActivity;
import com.example.yt_app.Fragment.CategoryThreeFragment;
import com.example.yt_app.Fragment.ServiceThreeFragment;
import com.example.yt_app.R;
import com.example.yt_app.Urils.State;
import com.example.yt_app.Urils.sample.CustomViewPager;
import com.gyf.immersionbar.ImmersionBar;
import com.permissionx.guolindev.PermissionX;


import java.util.ArrayList;

import butterknife.BindView;

/**
 * @author geyifeng
 * @date 2017/5/8
 */
@SuppressWarnings("ALL")
public class FragmentThreeActivity  extends BaseActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {

    @BindView(R.id.viewPager)
    CustomViewPager viewPager;
    @BindView(R.id.ll_home)
    LinearLayout llHome;
    @BindView(R.id.ll_category)
    LinearLayout llCategory;
    @BindView(R.id.ll_service)
    LinearLayout llService;

    private ArrayList<Fragment> mFragments;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_fragment_one;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this).navigationBarColor(R.color.colorPrimary).init();
        requestPower();
    }

    @Override
    protected void initData() {
        mFragments = new ArrayList<>();
        AnimationUseActivity homeThreeFragment = new AnimationUseActivity();
        CategoryThreeFragment categoryThreeFragment = new CategoryThreeFragment();
        ServiceThreeFragment serviceThreeFragment = new ServiceThreeFragment();
        mFragments.add(homeThreeFragment);
        mFragments.add(categoryThreeFragment);
        mFragments.add(serviceThreeFragment);

        if (State.DeviceIDs == ""){
            AlertDialog.Builder builder=new AlertDialog.Builder(this);
            builder.setTitle("提示");
            builder.setMessage("您还没有设备，是否跳转添加设备？");
            builder.setIcon(R.mipmap.gantanhao);
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    startActivity(new Intent(getApplication(), SetActivity.class));

                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            AlertDialog alertDialog=builder.create();
            alertDialog.show();
        }

    }

    /**
     *  动态赋予权限
     * */
    public void requestPower() {
        PermissionX.init(this)
                .permissions(Manifest.permission.ACCESS_NETWORK_STATE
                        /*访问网络权限*/
                        ,Manifest.permission.INTERNET
                        ,Manifest.permission.ACCESS_COARSE_LOCATION
                        ,Manifest.permission.ACCESS_FINE_LOCATION
                        /*GD地图*/
                        ,Manifest.permission.ACCESS_COARSE_LOCATION
                        ,Manifest.permission.ACCESS_FINE_LOCATION
                        ,Manifest.permission.ACCESS_NETWORK_STATE
                        ,Manifest.permission.CHANGE_WIFI_STATE
                        ,Manifest.permission.INTERNET
                        ,Manifest.permission.READ_PHONE_STATE
                        ,Manifest.permission.WRITE_EXTERNAL_STORAGE
                        ,Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS
                        ,Manifest.permission.BLUETOOTH
                )

                .onExplainRequestReason((scope, deniedList) -> {
                    scope.showRequestReasonDialog(deniedList,"HomeApp 需要您同意以下授权才能正常使用","同意","拒绝");
                })
                .request(((allGranted, grantedList, deniedList) -> {
                    if (allGranted) {
                        if (State.Start == false){
                            //Toast.makeText(this, "您同意了所有权限!", Toast.LENGTH_SHORT).show();
                            State.Start = true ;
                        }
                        //createLog4jLocal();
                    } else {
                        Toast.makeText(this, "您拒绝了以下权限:"+deniedList, Toast.LENGTH_SHORT).show();
                        //finish();
                    }
                }));

    }
    @Override
    protected void initView() {
        AppManager.getInstance().addActivity(this);
        viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
        viewPager.setOffscreenPageLimit(3);
        llHome.setSelected(true);
    }

    @Override
    protected void setListener() {
        llHome.setOnClickListener(this);
        llCategory.setOnClickListener(this);
        llService.setOnClickListener(this);
        viewPager.addOnPageChangeListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_home:
                viewPager.setCurrentItem(0);
                tabSelected(llHome);
                break;
            case R.id.ll_category:
                viewPager.setCurrentItem(1);
                tabSelected(llCategory);
                break;
            case R.id.ll_service:
                viewPager.setCurrentItem(2);
                tabSelected(llService);
                break;


            default:
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                tabSelected(llHome);
                ImmersionBar.with(this).keyboardEnable(false).statusBarDarkFont(false).navigationBarColor(R.color.colorPrimary).init();
                break;
            case 1:
                tabSelected(llCategory);
                ImmersionBar.with(this).keyboardEnable(false).statusBarDarkFont(true, 0.2f).navigationBarColor(R.color.btn3).init();
                break;
            case 2:
                tabSelected(llService);
                ImmersionBar.with(this).keyboardEnable(false).statusBarDarkFont(false).navigationBarColor(R.color.btn13).init();
                break;

            default:
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    private void tabSelected(LinearLayout linearLayout) {
        llHome.setSelected(false);
        llCategory.setSelected(false);
        llService.setSelected(false);

        linearLayout.setSelected(true);
    }

    @SuppressWarnings("AliDeprecation")
    private class MyAdapter extends FragmentPagerAdapter {
        @SuppressWarnings("AliDeprecation")
        MyAdapter(FragmentManager fm) {
            //noinspection AliDeprecation
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }
    }
    /**
     *
     * 声明一个long类型变量：用于存放上一点击“返回键”的时刻
     *
     */
    private long mExitTime;
    @SuppressWarnings({"AlibabaUndefineMagicConstant", "AlibabaRemoveCommentedCode"})
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //判断用户是否点击了“返回键”
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //与上次点击返回键时刻作差
            //noinspection AlibabaUndefineMagicConstant
            if ((System.currentTimeMillis() - mExitTime) > 2000)
            {
                //大于2000ms则认为是误操作，使用Toast进行提示
                showMessage("再按一次退出程序");
                //Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                //并记录下本次点击“返回键”的时刻，以便下次进行判断

            } else {
                AppManager.getInstance().removeAllActivity();
                System.exit(0);


            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


}
